<?php
/**
 * Plugin Color: Sets new colors for text and background.
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Christopher Smith <chris@jalakai.co.uk>
 */

// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');

/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_uniimage extends DokuWiki_Syntax_Plugin {

	/**
	 * return some info
	 */
	function getInfo(){
		return array(
            'author' => 'Benjamin Eckstein',
            'email'  => 'umingo@googlemail.com',
            'date'   => '2011-08-06',
            'name'   => 'Sommer Uni Menu',
            'desc'   => 'Creates Dokuwiki Images',
            'url'    => 'http://www.umingo.de',
		);
	}

	function getType(){ return 'formatting'; }
	function getAllowedTypes() { return array('formatting', 'substition', 'disabled'); }
	function getSort(){ return 158; }
	function connectTo($mode) { $this->Lexer->addEntryPattern('<uniimage.*?>(?=.*?</uniimage>)',$mode,'plugin_uniimage'); }
	function postConnect() { $this->Lexer->addExitPattern('</uniimage>','plugin_uniimage'); }

	function getPType(){ 
		return 'block';
	}

	/**
	 * Handle the match
	 */
	function handle($match, $state, $pos, &$handler){
		 
		switch ($state) {
			case DOKU_LEXER_ENTER :
				list($color, $background) = preg_split("/\//u", substr($match, 6, -1), 2);
				if ($color = $this->_isValid($color)) $color = "color:$color;";
				if ($background = $this->_isValid($background)) $background = "background-color:$background;";
				return array($state, array($color, $background));

			case DOKU_LEXER_UNMATCHED :  return array($state, $match);
			case DOKU_LEXER_EXIT :       return array($state, '');
		}
		return array();
	}

	/**
	 * Create output
	 */
	function render($mode, &$renderer, $data) {
		if($mode == 'xhtml'){
			list($state,$match) = $data;
			switch ($state) {
				case DOKU_LEXER_ENTER :
					//list($color, $background) = $match;
					$renderer->doc .= "<div class='icaption_right'>";
					break;

				case DOKU_LEXER_UNMATCHED :
					//match is the text within the <menu> </menu> part
<<<<<<< .mine
					$renderer->doc .= $this->create_unicaptions(htmlentities($match)); break;
				case DOKU_LEXER_EXIT :       $renderer->doc .= "</div>"; break;
			}
			return true;
		}
		return false;
=======
					$renderer->doc .= $this->create_menu($match); break;
              case DOKU_LEXER_EXIT :       $renderer->doc .= "</ul>"; break;
            }
            return true;
        }
        return false;
    }
 
 function create_menu($match){
 
	//split by ; 
	$entries = explode(";",$match);
	$out = "";
	foreach($entries as $s){
		$s = trim($s);
		if(strpos($s, " ") === FALSE){
			$out .= "<li><a href=\"doku.php?id=".$s."\">" . tpl_pagetitle($s, true)  .  "</a></li>";
		}
		else {
			$arr = explode(" ",$s,2);
			$out .= "<li><a href=\"doku.php?id=".$arr[0]."\">" . $arr[1]  .  "</a></li>";
		}
			
>>>>>>> .r6
	}

	function create_unicaptions($match){

		//split by ;
		$entries = explode(";",$match);
		$out = "";
		foreach($entries as $s){
			$s = trim($s);
			$arr = explode(" ",$s,2);
			$img = $arr[0];
			$descr = (count($arr) > 1)? $arr[1] : "";
			$out.= "<div class='icaption_s'>";			
			$out.= "<a href=\"/lib/exe/detail.php?id=start&media=".$img."\" title=\"$descr\"><img
										alt=\"$descr\" class=\"content_image_s\"
										src=\"/lib/exe/fetch.php?w=258&amp;media=".$img."\"/></a>";			
			if($descr != ""){
				$out.= "<h6>$descr</h6>";
			}
			$out.= "</div>";				
		}
		return $out;
	}
	// validate color value $c
	// this is cut price validation - only to ensure the basic format is correct and there is nothing harmful
	// three basic formats  "colorname", "#fff[fff]", "rgb(255[%],255[%],255[%])"
	function _isValid($c) {
		$c = trim($c);

		$pattern = "/^\s*(
            ([a-zA-z]+)|                                #colorname - not verified
            (\#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}))|        #colorvalue
            (rgb\(([0-9]{1,3}%?,){2}[0-9]{1,3}%?\))     #rgb triplet
            )\s*$/x";

		if (preg_match($pattern, $c)) return trim($c);

		return "";
	}
}
?>